﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Infrastructure.Repository;
using BlackBBS.Enums;
using BlackBBS.Helpers;

namespace BlackBBS.Models.Data
{
    [Serializable]
    public class CallHistory : ModelBase
    {        
        public long UserId { get; set; }
        public string FromAddress { get; set; }
        public DateTime TimeLogin { get; set; }
        public DateTime TimeLogout { get; set; }

        public static IList<string> CallHistoryDetails(IServerRepository serverRepository, int numberOfCallHistoryToDisplay)
        {
            var chTemplate = string.Format("$[Date]$[Time]{0} $[Caller]{0} $[From]", "|");

            var result = new List<string>();

            result.Add(string.Format("{1}** Last {0} Callers (Descending) **{2}",
                                     numberOfCallHistoryToDisplay,
                                     ANSIColorHelper.AnsiColor(ANSIForeGroundColors.Yellow, ANSIBackgroundColors.Black),
                                     ANSIColorHelper.ResetToDefault()) + "\r\n");

            // Add the 'header'
            result.Add(string.Format("{1}Date       Time  {0} Caller              {0} From           {2}",
                                     "|",
                                     ANSIColorHelper.AnsiColor(ANSIForeGroundColors.Cyan, ANSIBackgroundColors.Black),
                                     ANSIColorHelper.ResetToDefault()) + "\r\n");
            
            // Add last 20            
            var data = serverRepository.CallHistories.GetAll()
                           .OrderByDescending(x => x.TimeLogin)
                           .Take(numberOfCallHistoryToDisplay);
            foreach (var callHistory in data)
            {
                var user = serverRepository.Users.Get(callHistory.UserId);

                var ch = chTemplate.Replace("$[Date]", callHistory.TimeLogin.ToString("MM/dd/yyyy "));
                ch = ch.Replace("$[Time]", callHistory.TimeLogin.ToString("HH:mm "));
                ch = ch.Replace("$[Caller]", user.Login.PadRight(20));
                ch = ch.Replace("$[From]", callHistory.FromAddress.PadRight(15));
                result.Add(ch + "\r\n");
            }
            return result;
        }

        public static IList<string> TodaysCallers(IServerRepository serverRepository, int maximumCallHistory = 25)
        {
            var data = serverRepository.CallHistories.GetAll()
                       .Where(x => x.TimeLogin.ToString("MM/dd/yyyy").Equals(DateTime.Now.ToString("MM/dd/yyyy")))
                       .OrderByDescending(x => x.TimeLogin)
                       .Take(maximumCallHistory);                       

            var chTemplate = string.Format("$[Date]$[Time]{0} $[Caller]{0} $[From]", "|");

            var result = new List<string>();
            foreach (var callHistory in data)
            {
                var user = serverRepository.Users.Get(callHistory.UserId);

                var ch = chTemplate.Replace("$[Date]", callHistory.TimeLogin.ToString("MM/dd/yyyy "));
                ch = ch.Replace("$[Time]", callHistory.TimeLogin.ToString("HH:mm "));
                ch = ch.Replace("$[Caller]", user.Login.PadRight(20));
                ch = ch.Replace("$[From]", callHistory.FromAddress.PadRight(15));
                result.Add(ch + "\r\n");
            }
            return result;
        }

    }
}
